package DAO;

import java.io.File;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;

import java.io.*;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
 
public class Decryptage{
    
    
    /*
     
    code pour decrypter un fichier (a mettre dans le main ou l'interface graphique): 
    
    Decryptage d = new Decryptage();
    
    Path path = Paths.get("lolo.xml");
    * 
            try {
                byte[] data = Files.readAllBytes(path);
                System.out.println(d.decryptInString(data));
            } catch (IOException ex) {
                Logger.getLogger(AccueilListener.class.getName()).log(Level.SEVERE, null, ex);
            }
*/



    
      public final static int KEY_SIZE = 128;  // [32..448]

  private Key secretKey;
  private static String password="toto";
  
  
  public Decryptage() {
      
          try {
      KeyGenerator keyGen = KeyGenerator.getInstance("Blowfish");
      keyGen.init(KEY_SIZE);
      secretKey = new SecretKeySpec(password.getBytes(), "Blowfish");
    }
    catch (Exception e) {System.out.println(e);} 
 
  }

  public byte[] decryptInBytes(byte[] ciphertext) {
    try {
      Cipher cipher = Cipher.getInstance("Blowfish");
      cipher.init(Cipher.DECRYPT_MODE, secretKey);
      return cipher.doFinal(ciphertext);
    }
    catch (Exception e) {System.out.println(e);} 
    return null;
  }
  
  
  public String decryptInString(byte[] ciphertext) {
    return new String(decryptInBytes(ciphertext));
  }


}